//
//  OYNavigationController.swift
//  weiboCopycat
//
//  Created by Orange Yu on 2016/11/30.
//  Copyright © 2016年 Orange Yu. All rights reserved.
//

import UIKit

class OYNavigationController: UINavigationController,UIGestureRecognizerDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 设置手势代理
        let target = self.interactivePopGestureRecognizer?.delegate
//        print(self.interactivePopGestureRecognizer!)
        let pan = UIPanGestureRecognizer(target: target, action: Selector(("handleNavigationTransition:")))
        
//        let pan = UIPanGestureRecognizer(target: target, action:Selector(("handleNavigationTransition:")))
        pan.delegate = self
        
        self.view.addGestureRecognizer(pan)
        
        self.interactivePopGestureRecognizer?.isEnabled = false
        
    }
    
    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        if self.childViewControllers.count == 1 {
            return false
        }
        return true
    }

    override func pushViewController(_ viewController: UIViewController, animated: Bool) {
        
        
        if viewControllers.count > 0 {
            viewController.hidesBottomBarWhenPushed = true
        }
        
        super.pushViewController(viewController, animated: animated)
    
        if self.viewControllers.count > 1{
            
            if self.viewControllers.count == 2 {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: viewControllers.first!.navigationItem.title!, imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
   
                
            } else {
                viewController.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "返回", imageName: "navigationbar_back_withtext", target: self, action: #selector(popAction))
            }
        }
        
    }
    
    func popAction()  {
        self.popViewController(animated: true)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    



}
